import pytest
import allure
from time import sleep
from pages.page import HomePage, LoginInformation, InvoiceCollection


@allure.feature("票账税6.0")
class TestCase:
    @allure.story("新版登录验证")
    @pytest.mark.parametrize("company", [
        # "浙江新版验证自动化测试",
        # "浙江云取票测试166",
        # "天津新版验证自动化测试",
        # "湖北新版验证自动化测试",
        # "广东地区云取票测试161",
        # "广东云取票测试062",
        # "广东小规模季度采集验证",
        # "浙江云取票测试111",
        # "湖北地区测试企业008",
        "广东新版验证自动化测试"
    ])
    def test_certify(self, user_driver, company):
        with allure.step("关闭弹窗"):
            HomePage(user_driver).pop_up_window_manage()
        with allure.step("判断我知道了提示是否弹出"):
            HomePage(user_driver).tip_manage()
        with allure.step("选择测试圈子"):
            HomePage(user_driver).select_circle()
        with allure.step("关闭弹窗"):
            HomePage(user_driver).pop_up_window_manage()
        with allure.step("进入票税登录信息页面"):
            HomePage(user_driver).enter_login_information()
        with allure.step("输入企业名称并搜索"):
            LoginInformation(user_driver).input_select(company)
        with allure.step("点击企业名称进入登录信息页面"):
            LoginInformation(user_driver).click_company_name()
        with allure.step("登录信息页面发起税局重新验证"):
            LoginInformation(user_driver).recertify()
        with allure.step("关闭登录信息页面"):
            LoginInformation(user_driver).close()
        with allure.step("刷新并判断税局验证状态"):
            count = 1
            while count < 10:
                count += 1
                sleep(10)
                HomePage(user_driver).tip_manage()
                LoginInformation(user_driver).enter_login_information_iframe()
                LoginInformation(user_driver).refresh()
                status = LoginInformation(user_driver).certify_status()
                if status == "验证中":
                    print("resume")
                    continue
                elif status == "验证成功":
                    print("success")
                    break
                else:
                    print("fail")
                    break
        with allure.step("断言"):
            assert '验证成功' == LoginInformation(user_driver).certify_status()

    @allure.story("发票采集")
    @pytest.mark.parametrize("company_name", [
        # "湖北地区测试企业008",
        # "浙江云取票测试166",
        "广东新版验证自动化测试"
        ])
    def test_invoice_collection(self, user_driver, company_name):
        with allure.step("进入发票采集页面"):
            HomePage(user_driver).switch_to_default_content()
            HomePage(user_driver).enter_invoice_collection()
        with allure.step("输入客户名称并搜索"):
            InvoiceCollection(user_driver).input_company_search(company_name)
        with allure.step("勾选取票客户"):
            InvoiceCollection(user_driver).opt_for_company()
        with allure.step("点击发票采集"):
            InvoiceCollection(user_driver).click_invoice_collection()
        with allure.step("循环刷新"):
            count = 1
            while count < 10:
                count += 1
                sleep(60)
                InvoiceCollection(user_driver).click_refresh()
                output_status = InvoiceCollection(user_driver).output_status_return()
                if output_status == "采集中":
                    print("resume")
                    continue
                else:
                    print("success")
                    break
        with allure.step("断言"):
            assert "2023" in InvoiceCollection(user_driver).output_status_return()
